/*
 * Copyright (c) 2013, marco.tamburelli@gmail.com
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met: 
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.bmi.gwt.mi.client.impl;

import java.util.Date;

import org.bmi.gwt.mi.shared.communication.ptypes.ArrayParam;
import org.bmi.gwt.mi.shared.communication.ptypes.BooleanParam;
import org.bmi.gwt.mi.shared.communication.ptypes.ByteParam;
import org.bmi.gwt.mi.shared.communication.ptypes.CharacterParam;
import org.bmi.gwt.mi.shared.communication.ptypes.DateParam;
import org.bmi.gwt.mi.shared.communication.ptypes.DoubleParam;
import org.bmi.gwt.mi.shared.communication.ptypes.FloatParam;
import org.bmi.gwt.mi.shared.communication.ptypes.IntegerParam;
import org.bmi.gwt.mi.shared.communication.ptypes.LongParam;
import org.bmi.gwt.mi.shared.communication.ptypes.ObjectParam;
import org.bmi.gwt.mi.shared.communication.ptypes.ObjectVarParam;
import org.bmi.gwt.mi.shared.communication.ptypes.ShortParam;
import org.bmi.gwt.mi.shared.communication.ptypes.StringParam;
import org.bmi.gwt.mi.shared.communication.ptypes.array.BooleanArrayWrapper;
import org.bmi.gwt.mi.shared.communication.ptypes.array.ByteArrayWrapper;
import org.bmi.gwt.mi.shared.communication.ptypes.array.CharacterArrayWrapper;
import org.bmi.gwt.mi.shared.communication.ptypes.array.DateArrayWrapper;
import org.bmi.gwt.mi.shared.communication.ptypes.array.DoubleArrayWrapper;
import org.bmi.gwt.mi.shared.communication.ptypes.array.FloatArrayWrapper;
import org.bmi.gwt.mi.shared.communication.ptypes.array.IntegerArrayWrapper;
import org.bmi.gwt.mi.shared.communication.ptypes.array.LongArrayWrapper;
import org.bmi.gwt.mi.shared.communication.ptypes.array.ObjectArrayWrapper;
import org.bmi.gwt.mi.shared.communication.ptypes.array.ShortArrayWrapper;
import org.bmi.gwt.mi.shared.communication.ptypes.array.StringArrayWrapper;
import org.bmi.gwt.mi.shared.exceptions.InvalidArgumentException;
import org.bmi.gwt.mi.shared.util.ArrayUtils;
import org.bmi.gwt.mi.shared.util.ProxyBase;

import com.google.gwt.user.client.rpc.IsSerializable;

/** For internal use only */
public class ParamUtils
{
	public static BooleanParam createBooleanParam(Boolean p)
	{
		BooleanParam _p = new BooleanParam();
		_p.setValue(p);

		return _p;
	}

	public static ByteParam createByteParam(Byte p)
	{
		ByteParam _p = new ByteParam();
		_p.setValue(p);

		return _p;
	}

	public static CharacterParam createCharacterParam(Character p)
	{
		CharacterParam _p = new CharacterParam();
		_p.setValue(p);

		return _p;
	}

	public static DateParam createDateParam(Date p)
	{
		DateParam _p = new DateParam();
		_p.setValue(p);

		return _p;
	}

	public static DoubleParam createDoubleParam(Double p)
	{
		DoubleParam _p = new DoubleParam();
		_p.setValue(p);

		return _p;
	}

	public static FloatParam createFloatParam(Float p)
	{
		FloatParam _p = new FloatParam();
		_p.setValue(p);

		return _p;
	}

	public static IntegerParam createIntegerParam(Integer p)
	{
		IntegerParam _p = new IntegerParam();
		_p.setValue(p);

		return _p;
	}

	public static LongParam createLongParam(Long p)
	{
		LongParam _p = new LongParam();
		_p.setValue(p);

		return _p;
	}

	public static ShortParam createShortParam(Short p)
	{
		ShortParam _p = new ShortParam();
		_p.setValue(p);

		return _p;
	}

	public static StringParam createStringParam(String p)
	{
		StringParam _p = new StringParam();
		_p.setValue(p);

		return _p;
	}

	public static ObjectVarParam createProxyParam(ProxyBase p)
	{
		ObjectVarParam _p = new ObjectVarParam();
		_p.init(ProxyBase.getObjectId(p));

		return _p;
	}

	public static ObjectParam createObjectParam(IsSerializable p)
	{
		ObjectParam _p = new ObjectParam();
		_p.setValue(p);

		return _p;
	}

	public static ArrayParam createArrayParam(boolean[] array)
	{
		BooleanArrayWrapper wrapper = new BooleanArrayWrapper();
		wrapper.setValues(ArrayUtils.asList(array));

		ArrayParam _p = new ArrayParam();
		_p.setValue(wrapper);

		return _p;
	}

	public static ArrayParam createArrayParam(byte[] array)
	{
		ByteArrayWrapper wrapper = new ByteArrayWrapper();
		wrapper.setValues(ArrayUtils.asList(array));

		ArrayParam _p = new ArrayParam();
		_p.setValue(wrapper);

		return _p;
	}

	public static ArrayParam createArrayParam(char[] array)
	{
		CharacterArrayWrapper wrapper = new CharacterArrayWrapper();
		wrapper.setValues(ArrayUtils.asList(array));

		ArrayParam _p = new ArrayParam();
		_p.setValue(wrapper);

		return _p;
	}

	public static ArrayParam createArrayParam(double[] array)
	{
		DoubleArrayWrapper wrapper = new DoubleArrayWrapper();
		wrapper.setValues(ArrayUtils.asList(array));

		ArrayParam _p = new ArrayParam();
		_p.setValue(wrapper);

		return _p;
	}

	public static ArrayParam createArrayParam(float[] array)
	{
		FloatArrayWrapper wrapper = new FloatArrayWrapper();
		wrapper.setValues(ArrayUtils.asList(array));

		ArrayParam _p = new ArrayParam();
		_p.setValue(wrapper);

		return _p;
	}

	public static ArrayParam createArrayParam(int[] array)
	{
		IntegerArrayWrapper wrapper = new IntegerArrayWrapper();
		wrapper.setValues(ArrayUtils.asList(array));

		ArrayParam _p = new ArrayParam();
		_p.setValue(wrapper);

		return _p;
	}

	public static ArrayParam createArrayParam(long[] array)
	{
		LongArrayWrapper wrapper = new LongArrayWrapper();
		wrapper.setValues(ArrayUtils.asList(array));

		ArrayParam _p = new ArrayParam();
		_p.setValue(wrapper);

		return _p;
	}

	public static ArrayParam createArrayParam(short[] array)
	{
		ShortArrayWrapper wrapper = new ShortArrayWrapper();
		wrapper.setValues(ArrayUtils.asList(array));

		ArrayParam _p = new ArrayParam();
		_p.setValue(wrapper);

		return _p;
	}

	// --------------

	public static ArrayParam createArrayParam(Byte[] array)
	{
		ByteArrayWrapper wrapper = new ByteArrayWrapper();
		wrapper.setValues(ArrayUtils.asList(array));

		ArrayParam _p = new ArrayParam();
		_p.setValue(wrapper);

		return _p;
	}

	public static ArrayParam createArrayParam(Character[] array)
	{
		CharacterArrayWrapper wrapper = new CharacterArrayWrapper();
		wrapper.setValues(ArrayUtils.asList(array));

		ArrayParam _p = new ArrayParam();
		_p.setValue(wrapper);

		return _p;
	}

	public static ArrayParam createArrayParam(Date[] array)
	{
		DateArrayWrapper wrapper = new DateArrayWrapper();
		wrapper.setValues(ArrayUtils.asList(array));

		ArrayParam _p = new ArrayParam();
		_p.setValue(wrapper);

		return _p;
	}

	public static ArrayParam createArrayParam(Double[] array)
	{
		DoubleArrayWrapper wrapper = new DoubleArrayWrapper();
		wrapper.setValues(ArrayUtils.asList(array));

		ArrayParam _p = new ArrayParam();
		_p.setValue(wrapper);

		return _p;
	}

	public static ArrayParam createArrayParam(Float[] array)
	{
		FloatArrayWrapper wrapper = new FloatArrayWrapper();
		wrapper.setValues(ArrayUtils.asList(array));

		ArrayParam _p = new ArrayParam();
		_p.setValue(wrapper);

		return _p;
	}

	public static ArrayParam createArrayParam(Integer[] array)
	{
		IntegerArrayWrapper wrapper = new IntegerArrayWrapper();
		wrapper.setValues(ArrayUtils.asList(array));

		ArrayParam _p = new ArrayParam();
		_p.setValue(wrapper);

		return _p;
	}

	public static ArrayParam createArrayParam(Long[] array)
	{
		LongArrayWrapper wrapper = new LongArrayWrapper();
		wrapper.setValues(ArrayUtils.asList(array));

		ArrayParam _p = new ArrayParam();
		_p.setValue(wrapper);

		return _p;
	}

	public static ArrayParam createArrayParam(Short[] array)
	{
		ShortArrayWrapper wrapper = new ShortArrayWrapper();
		wrapper.setValues(ArrayUtils.asList(array));

		ArrayParam _p = new ArrayParam();
		_p.setValue(wrapper);

		return _p;
	}

	public static ArrayParam createArrayParam(String[] array)
	{
		StringArrayWrapper wrapper = new StringArrayWrapper();
		wrapper.setValues(ArrayUtils.asList(array));

		ArrayParam _p = new ArrayParam();
		_p.setValue(wrapper);

		return _p;
	}

	public static ArrayParam createArrayParam(IsSerializable[] array)
	{
		ObjectArrayWrapper wrapper = new ObjectArrayWrapper();
		wrapper.setValues(ArrayUtils.asList(array));

		ArrayParam _p = new ArrayParam();
		_p.setValue(wrapper);

		return _p;
	}

	/**
	 * Creates an {@link ArrayParam} assuming that the provided argument is a
	 * higher dimensional array. If not an {@link InvalidArgumentException} will
	 * be thrown.
	 * 
	 * @param p An array whose components can be recognized and serialized by
	 *        the BMI.
	 * @return
	 * @throws InvalidArgumentException
	 */
	public static ArrayParam createArrayParam(Object[] p) throws InvalidArgumentException
	{
		ArrayParam _p = new ArrayParam();
		_p.setValue(ArrayUtils.wrapArray(p));

		return _p;
	}
}
